import axios from 'axios'
import dsktConstant from '../../config/constant'
import cookie from '../../assets/js/cookie'

export default {
	getUrlKey: function (name) {
		//获取url 参数
		return (
			decodeURIComponent(
				(new RegExp("[?|&]" + name + "=" + "([^&;]+?)(&|#|;|$)").exec(
					location.href
				) || [, ""])[1].replace(/\+/g, "%20")
			) || null
		);
	},
	getWxAppId: function (schoolNo) {
		//获取url 参数
		return axios.get('/basic/sys_info/school/wechat?no=' + dsktConstant.SYS_NO + '&schoolNo=' + schoolNo);
	},
	getAuthUrl() {
		var locationHref = window.location.href;
		let urlNow = encodeURIComponent(locationHref);
		let scope = 'snsapi_userinfo';    //snsapi_userinfo  snsapi_base //静默授权 用户无感知
		let state = 'dskt';    //参数 随便
		let appid = cookie.getAppid();
		let url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${urlNow}&response_type=code&scope=${scope}&state=${state}#wechat_redirect`;
		return url;
	},
	//通过新by code 
	getInfoByBode(code) {
		var appId = cookie.getAppid();
		///wechat/{appid}/greet
		var reqData = {
			code: code
		};
		let param = new URLSearchParams();
		for (var key in reqData) {
			param.append(key, reqData[key])
		}
		return axios({
			url: '/basic/wechat/' + appId + '/greet',
			method: 'post',
			data: param,
		});

	},
	//通过新by code 
	getSignature(url) {
		var appId = cookie.getAppid();
		return axios.post('/basic/wechat/' + appId + '/signature?url='+url);
	},
	initWx(wx) {
		/*	var url = location.href;
		var appId = cookie.getAppid();
	//	console.log(url);
 	axios.post('/basic/wechat/' + appId + '/signature?url=' + url).then(res => {
		  if (res.data) {
		    var data = res.data;
		    wx.config({
		      debug: false,
		      appId: data.appId, // 必填，企业号的唯一标识，此处填写企业号corpid
		      timestamp: data.timestamp, // 必填，生成签名的时间戳
		      nonceStr: data.nonceStr, // 必填，生成签名的随机串
		      signature: data.signature, // 必填，签名，见附录1
		      jsApiList: ["scanQRCode", "updateAppMessageShareData",
		          'updateTimelineShareData',
		          'onMenuShareAppMessage',
		          'onMenuShareTimeline'] //旧的接口，即将废弃] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2 //旧的接口，即将废弃
			});

			wx.ready(function () {
			  //微信分享菜单测试
			  wx.onMenuShareAppMessage({
			    title: "第三课堂", // 分享标题
			    desc: "第三课堂", // 分享描述
			    link: "http://tempdev.2ndclass.cn/3rdclassroom/#/" + cookie.get(cookie.key.schoolNo) + "/login", // 分享链接
			    imgUrl: "", // 分享图标
			    success: function () {
			   //    alert("成功分享给朋友")
			    },
			    cancel: function () {
			  //     alert("分享失败,您取消了分享!")
			    }
			  });
			});
		  }
		}); */

	},

}